
#!bin/bash

g_last_level=-1
g_last_cpu_list=""
g_level=1
g_cpu_list=""

function get_watt_state() {
         re=$(cat /sys/fs/cgroup/cpu/test_watt/cpu.dynamic_affinity_mode)
         return $re
}

function update_curr_level() {
         content=`cat /sys/fs/cgroup/cpu/test_watt/cpu.affinity_stat | grep "curr_level" | awk '{print $2}'`
         g_level=${content:0:1}
}

function update_cpu_list {
         keywords="sd_level "$g_level","
         content=$(cat /sys/fs/cgroup/cpu/test_watt/cpu.affinity_stat | grep "${keywords}")
         content=${content#*cpu list }
         g_cpu_list=${content%%, stay_cnt*}
}

function main () {
         update_curr_level
         echo "g_level:"$g_level"---"
         update_cpu_list
         # echo "g_cpu_list:"$g_cpu_list"--"
         # echo "g_last_level:"$g_last_level"---"
         # echo "g_last_cpu_list:"$g_last_cpu_list"---"       
         if [ $g_level \> $g_last_level ];then
                   echo "scale out. last:"$g_last_cpu_list"  current:"$g_cpu_list
                   cpupower -c $g_cpu_list frequency-set -g performance >/dev/null
         elif [ $g_level \< $g_last_level ];then
                   echo "scale in.  last:"$g_last_cpu_list"  current:"$g_cpu_list
                   cpupower frequency-set -g conservative >/dev/null
                   cpupower -c $g_cpu_list frequency-set -g performance >dev/null
         fi
         
         g_last_level=$g_level
         g_last_cpu_list=$g_cpu_list
}

while true;do
         get_watt_state >/dev/null
         if [ $? == "1" ];then
                   main
         fi
         sleep 2
done


